package com.sky.com.sky.web.admin;

import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 **/

@RestController
@RequestMapping("/api/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

   @Autowired
   private EmployeeService employeeService;

   @Autowired
   private JwtProperties jwtProperties;



    /**
     * 登录
     *
     * @param empolyeeDTO
     * @return
     * **/

    @PostMapping("/login")
    @ApiOperation("员工登录")
    @ApiImplicitParam(name="employeeDTO",value = "员工DTO",required = true,example = "username:admin,password=123456")
    public Result login(@RequestBody EmployeeDTO empolyeeDTO){
       // 调用service
        Employee employee = employeeService.login(empolyeeDTO);
        //jwt制作token
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("empId",employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecret(), jwtProperties.getAdminTtl(),
                claims);
        //返回vo
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .name(employee.getName())
                .token(token)
                .userName(employee.getUsername()).build();
        return Result.success(employeeLoginVO);
    }

    /**
     * 推出
     *
     * @return
     * */
   @PostMapping("/logout")
   @ApiOperation("退出登录")
   public Result logout(){
       return Result.success();
   }

    /**
     * 添加员工
     * @param employeeDTO
     * @return
     * **/
   @PostMapping
   public Result saave(@RequestBody EmployeeDTO employeeDTO){
        //调用service
       employeeService.save(employeeDTO);
       // 返回sucess
       return Result.success();
   }

    /**
     * 员工分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     * **/
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "员工名称", required = false, example = "波仔"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, defaultValue = "1", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, defaultValue = "5", example = "5")
    })
    public Result page(String name,
                       @RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        PageResult pageResult=employeeService.page(name,page,pageSize);
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     * **/
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id){
        //调用service
        Employee employee = employeeService.getById(id);
        // 返回
        return Result.success(employee);
    }


    /**
     * 修改
     * @param employueeDTO
     * @return
     * **/
    @PutMapping
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        //调用service
        employeeService.update(employeeDTO);
        //返回sucess
        return Result.success();
    }


    /**
     * 账号启用禁用
     * @param status
     * @param id
     * @return
     * **/
    @PostMapping("/status/{status}")
    public Result startOrStop(
        @PathVariable("status") Integer status,
                @RequestParam Long id ){
        //调用service
        employeeService.startOrStop(status,id);
        //返回sucess
        return Result.success();
    }




}